package com.animator.learn;

import android.animation.TimeInterpolator;
import android.animation.ValueAnimator;

public class TypeEvaluator {
	public class Point{
		private float x;
		private float y;
		
		public Point(float x, float y) {
			this.x = x;
			this.y = y;
		}

		public float getX() {
			return x;
		}

		public float getY() {
			return y;
		}

	}
	
	public class PointEvaluator implements android.animation.TypeEvaluator<Object>{

		@Override
		public Object evaluate(float fraction, Object startValue, Object endValue) {
			Point startPoint = (Point)startValue;
			Point endPoint = (Point)endValue;
			
			float x = startPoint.getX() + fraction * (endPoint.getX() - startPoint.getX());
			float y = startPoint.getY() + fraction * (endPoint.getY() - startPoint.getY());
		
			Point point = new Point(x, y);
			return point;
		}
		
	}
	
	/**
	 * 自定义插值器(先减速，后加速)
	 * @description:描述该类的主要功能
	 * @author zhonghong.zhangxg
	 * @date 2016年6月30日下午3:38:39
	 */
	public class DecelerateAccelerateInterpolator implements TimeInterpolator {

		@Override
		public float getInterpolation(float input) {
			float result;
			if (input <= 0.5) {
				result = (float) (Math.sin(Math.PI * input)) / 2;
			} else {
				result = (float) (2 - Math.sin(Math.PI * input)) / 2;
			}
			return result;
		}
		
	};
	
	public void test()	{
		Point point1 = new Point(0,0);
		Point point2 = new Point(300, 300);
		ValueAnimator anim = ValueAnimator.ofObject(new PointEvaluator(), point1, point2);
		anim.setDuration(500);
		anim.setInterpolator(new DecelerateAccelerateInterpolator());
		anim.start();
	}
	
	

}
